package com.ity.teamanagement.utils;

import com.ity.teamanagement.code.ImgCode;
import com.ity.teamanagement.entity.outputResult.QrCodeList;
import com.ity.teamanagement.exception.ExceptionUtils;
import io.swagger.annotations.ApiModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author coderFu
 * @create 2021/6/2
 */
@ApiModel("文件工具类")
public class FileUtils {

    /**
     * 最大文件上传大小为5MB
     */
    private static final long IMG_SIZE = 15*1024*1024;

    /**
     * 图片存储文件夹
     */
    static  String baseName = "tea_images";
    /**
     * 图片存储路径   "/../" 返回上一级目录
     */
    static String imgPath = System.getProperty("user.dir")+"/../"+baseName+"";
    static String selfPath = System.getProperty("user.dir")+"/../";

    /**
     * 临时图片的路径
     */
    private static final String TEMP_QR_CODE_PATH = System.getProperty("user.dir")+"/../"+File.separator+"imgTempFile";

   static public String getTempQrCodePath(){
       return TEMP_QR_CODE_PATH;
   }
    /**
     * 获取图片的完整路径
     * @param path
     * @return
     */
    static public String getAllImgPath(String path){
        return selfPath +File.separator+ path;
    }

    /**
     * 上传图片
     * @param uploadFile
     * @return
     */
    public static String uploadFile(MultipartFile uploadFile){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
        //20210604164709972 获得当前时间的值
        String res = sdf.format(new Date());
        //设置保存文件的路径
        String rootPath = imgPath;
        //原始文件名称
        String originalFilename = uploadFile.getOriginalFilename();
        //生成保存的文件名称
        String newFileName = "";
        if (StringUtils.isEmpty(originalFilename)){
            newFileName = res + UUIDUtils.getUUID().toString() +".jpg";
        }else {
            newFileName = res + UUIDUtils.getUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
        }
        //创建年月日文件夹
        Calendar date =Calendar.getInstance();
        File dateDirs = new File(date.get(Calendar.YEAR)
                + File.separator + (date.get(Calendar.MONTH) + 1)
                + File.separator + (date.get(Calendar.DATE)));
        //目标文件所在路径
        String desFilePath =  File.separator + dateDirs + File.separator + newFileName;
        //完整存储路径
        File newFile = new File(rootPath +desFilePath);
        /**
         * 判断月份或者天数是否存在，不存在则生成
         */
        if (!newFile.getParentFile().exists()) {
            //如果目标文件所在的目录不存在，则创建父目录
            newFile.getParentFile().mkdirs();
        }
        try {
            uploadFile.transferTo(newFile);
        } catch (IOException e) {
            ExceptionUtils.error(ImgCode.IMG_UPDATE_ERROR);
        }
        //返回图片存储路径
        return baseName+desFilePath;
    }

    /**
     * 根据文件路径删除文件
     * @param path
     */
    public static boolean deleteFile(String path) {
        String allImgPath = getAllImgPath(path);
        File file = new File(allImgPath);
        if (file.exists()){
            return file.delete();
        }
        return false;
    }

    /**
     * 更改文件的名称
     * @param src
     * @return
     */
    public static File changeFileName(String src, QrCodeList qrCodeList){
        File srcFile = new File(src);
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        File desFile = null;
        byte[] buffer;
        int len;
        buffer = new byte[2048];
        try {
            bis = new BufferedInputStream(new FileInputStream(srcFile));
            String desPath = TEMP_QR_CODE_PATH+File.separator+qrCodeList.getChineseName()+" --"+UUIDUtils.getUUID().toString()+".jpg";
            desFile = new File(desPath);
            if (!desFile.getParentFile().exists()) {
                //如果目标文件所在的目录不存在，则创建父目录
                desFile.getParentFile().mkdirs();
            }
            bos = new BufferedOutputStream(new FileOutputStream(desFile));
            while ((len = bis.read(buffer))!=-1){
                bos.write(buffer,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

                try {
                    if (bis !=null) {
                        bis.close();
                    }
                    if (bos !=null){
                        bos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }return desFile;
    }



    /**
     *  检查文件大小，不能超过5M
     * @param multipartFile
     */
    public static boolean  checkImgSize(MultipartFile multipartFile){
//        if (multipartFile.getSize()>IMG_SIZE){
//            return false;
//        }
        return true;
    }


}
